package com.sphd.emanu.activity.goodslist;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.sphd.emanu.R;
import com.sphd.emanu.activity.gooddetails.GooddetailsActivity;
import com.sphd.emanu.activity.goodsfrom.GoodsfromActivity;
import com.sphd.emanu.adapter.GoodslistAdapter;
import com.sphd.emanu.config.Constant;
import com.sphd.emanu.mvp.MVPBaseActivity;
import com.sphd.emanu.permission.Moudle;
import com.sphd.emanu.pojo.goods.Category;
import com.sphd.emanu.pojo.goods.Commodity;
import com.sphd.emanu.pojo.login.User;
import com.sphd.emanu.utils.PermissionConfig;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.pedant.SweetAlert.SweetAlertDialog;

/**
 * 商品的列表的操作
 *
 * @Author Hu
 * @Time 2017/4/28 -- 16:46.
 * @Email 87683202@qq.com
 */
public class GoodslistActivity extends MVPBaseActivity<GoodslistContract.View, GoodslistPresenter> implements GoodslistContract.View {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.my_recycler)
    RecyclerView myRecycler;
    @BindView(R.id.my_swf)
    SwipeRefreshLayout mySwf;
    GoodslistAdapter goodslistAdapter;
    Intent intent;
    Bundle bundle;
    Category tMtCategory;
    @BindView(R.id.add_good)
    TextView addGood;
    User userEntity;
    List<Moudle> moudles;
    boolean isCanDo=false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_goodslist);
        ButterKnife.bind(this);
        initView();
        initEvent();
    }

    private void initEvent() {
        toolbar.setNavigationOnClickListener(v -> finish());
        myRecycler.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {
                //商品详情
                bundle.putSerializable(Constant.FLAG,1);
                bundle.putSerializable(Constant.COMMODITY, (Commodity) adapter.getItem(position));
                intent.setClass(context, GooddetailsActivity.class);
                intent.putExtras(bundle);
                startActivityForResult(intent,Constant.REQUEST_CODE);
            }

            @Override
            public void onItemLongClick(BaseQuickAdapter adapter, View view, final int position) {
                super.onItemLongClick(adapter, view, position);
               final Commodity goodsEntity= (Commodity) adapter.getItem(position);
                new SweetAlertDialog(context,SweetAlertDialog.WARNING_TYPE).setTitleText("删除")
                        .setContentText("确定要删除"+goodsEntity.getName()+"商品吗？").setConfirmText("确定").setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
                    @Override
                    public void onClick(SweetAlertDialog sweetAlertDialog) {
                        mPresenter.delGood(goodsEntity.getId(),sweetAlertDialog,position);
                    }
                }).setCancelText("取消").setCancelClickListener(new SweetAlertDialog.OnSweetClickListener() {
                    @Override
                    public void onClick(SweetAlertDialog sweetAlertDialog) {
                        sweetAlertDialog.dismiss();
                    }
                }).show();

            }

            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                super.onItemChildClick(adapter, view, position);
                //跳转到商品构成
                bundle.putSerializable(Constant.COMMODITY, (Commodity) adapter.getItem(position));
                intent.setClass(context, GoodsfromActivity.class);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
        mySwf.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                addData();
            }
        });

    }

    private void initView() {
        setTitleBar(toolbar, "商品");
        setRefresh(mySwf);
        intent = getIntent();
        bundle = intent.getExtras();
        tMtCategory = (Category) bundle.getSerializable(Constant.CATEGORY);
        userEntity = (User) bundle.getSerializable(Constant.USER_ENTITY);
        moudles= (List<Moudle>) bundle.getSerializable(Constant.PERMISSION_LIST);
        isCanDo=isCanDo(moudles, PermissionConfig.MENU_FOOD_1);
        if (isCanDo){
            addGood.setVisibility(View.VISIBLE);
        }
        initAdapter();
        addData();
    }

    private void addData() {
        showProgressDialog();
        mPresenter.getList(tMtCategory.getId());
    }

    private void initAdapter() {
        setRecy(myRecycler);
        goodslistAdapter = new GoodslistAdapter(R.layout.item_goods_list, new ArrayList<Commodity>(), context);
        goodslistAdapter.setEmptyView(emptyView);
        myRecycler.setAdapter(goodslistAdapter);
    }

    @Override
    public void setList(List<Commodity> commodityList) {
        goodslistAdapter.setNewData(commodityList);
        dissProgressDialog();
    }

    @Override
    public void setDelRes(int delRes, SweetAlertDialog sweetAlertDialog, int pos) {
        if (delRes==1){
            toast("删除成功");
            sweetAlertDialog.dismiss();
            goodslistAdapter.remove(pos);
        }else {
            toast("删除失败，请重试");
        }
    }

    /**
     * 新增商品和商品详情共用界面
     */
    @OnClick(R.id.add_good)
    public void onViewClicked() {
        bundle.putSerializable(Constant.FLAG,0);
        bundle.putSerializable(Constant.COMMODITY, null);
        intent.setClass(context, GooddetailsActivity.class);
        intent.putExtras(bundle);
        startActivityForResult(intent,Constant.REQUEST_CODE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode==Constant.REQUEST_CODE){
            addData();
        }
    }
}
